package com.sino.second.struts2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;


import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.IBasicInfoService;
import com.sino.business.IPatentManageService;
import com.sino.business.IStaticDataService;
import com.sino.model.BasicInfoModel;
import com.sino.second.bean.TblEmailset;
import com.sino.second.business.IEmailService;
import com.sino.second.exception.BusinessException;
import com.sino.second.util.FileManager;
import com.sino.second.util.MailSendUtils;
import com.sino.second.util.MailSettingForm;
import com.sino.util.SysParameter;
import com.sino.util.page.PagerModel;
public class EmailSendAction extends ActionSupport{
	IEmailService  emailServiceImpl;//邮件发送
	private IStaticDataService staticDateService;// 基础数据
	private IBasicInfoService basicInfoService;// 基本信息
	private IPatentManageService ipatentManageService;// 项目管理
	private TblEmailset emailSet; //邮件设置
	private Integer emailSetId;  //邮件ID
	private String year;//年度;
	private PagerModel pm;//
	private List xkList=null;//===学科||专业
	private List<BasicInfoModel>  bimList=null;
	private String xkId;//学科||专业ID
	private String [] emails;
	private String emailsString;
	private MailSettingForm  msf;
	


	/**
     * 发送邮件设置
     * @return
     */
	public String sendMailSet(){
		xkList=this.getStaticDateService().findXkList();
		pm=this.emailServiceImpl.findEmeilSetList("", SysParameter.CURRENT_YEAR);
	    this.setYear(SysParameter.CURRENT_YEAR);
		return this.SUCCESS;
	}
	/**
	 * 异步获项目信息
	 * @return
	 */
	public String listXmByXkId(){
        String isqualified = "basicinfo.xk.id='"
		+ xkId+ "' and basicinfo.year="
		+ SysParameter.CURRENT_YEAR;// 项目条件
        bimList = this.getIpatentManageService().getBasicInfoPageList(
		0, 1000, isqualified, "0000");// 分页查询考生信息
       return this.SUCCESS;
	}
	/**
	 * 批量发送邮件
	 * @return
	 * @throws Exception
	 */
	public String sendMails(){
		emails=emailsString.split(",");//分离邮件
	 	for(String te:emails){
	 		MailSettingForm mailForm = new MailSettingForm();
			mailForm.setContent(msf.getContent());
			mailForm.setMailaddr(msf.getMailaddr());
			mailForm.setPassword(msf.getPassword());
			mailForm.setReceive(te);
			int startAdd=msf.getMailaddr().indexOf("@");
			mailForm.setSmtp("smtp."+msf.getMailaddr().substring(startAdd+1));
			mailForm.setSubject(msf.getSubject());
			
			List<String> filePathList=new ArrayList<String>();
			String toFilePath = ServletActionContext.getRequest().getRealPath(
			"admin/sysparam/mailAttachment")+"//";
		   for(int i=0;i<msf.getFile().size();i++){
               try {
            	String toName=msf.getFileFileName().get(i);   
				FileManager.fileUpload(msf.getFile().get(i), toFilePath,toName);
				filePathList.add(toFilePath+toName);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			   
		   }
		   mailForm.setFileList(filePathList);
			try{
			MailSendUtils.sentMailHtml(mailForm);
			}catch(Exception e){
				e.printStackTrace();
				throw new BusinessException("sys.sendMails.exception");
			}
		}
		return this.SUCCESS;
	}
    
	

	public List<BasicInfoModel> getBimList() {
		return bimList;
	}
	public void setYear(String year) {
		this.year = year;
	}

	public void setBimList(List<BasicInfoModel> bimList) {
		this.bimList = bimList;
	}
	@JSON(serialize=false) //json不序列化
	public IEmailService getEmailServiceImpl() {
		return emailServiceImpl;
	}
	public void setEmailServiceImpl(IEmailService emailServiceImpl) {
		this.emailServiceImpl = emailServiceImpl;
	}
	@JSON(serialize=false) //json不序列化
	public TblEmailset getEmailSet() {
		return emailSet;
	}
	public void setEmailSet(TblEmailset emailSet) {
		this.emailSet = emailSet;
	}
	@JSON(serialize=false) //json不序列化
	public Integer getEmailSetId() {
		return emailSetId;
	}
	public void setEmailSetId(Integer emailSetId) {
		this.emailSetId = emailSetId;
	}
	@JSON(serialize=false) //json不序列化
	public IPatentManageService getIpatentManageService() {
		return ipatentManageService;
	}
	public void setIpatentManageService(IPatentManageService ipatentManageService) {
		this.ipatentManageService = ipatentManageService;
	}
	@JSON(serialize=false) //json不序列化
	public PagerModel getPm() {
		return pm;
	}
	public void setPm(PagerModel pm) {
		this.pm = pm;
	}
	@JSON(serialize=false) //不序列化
	public IStaticDataService getStaticDateService() {
		return staticDateService;
	}
	public void setStaticDateService(IStaticDataService staticDateService) {
		this.staticDateService = staticDateService;
	}
	@JSON(serialize=false) //json不序列化
	public String getXkId() {
		return xkId;
	}
	public void setXkId(String xkId) {
		this.xkId = xkId;
	}
	@JSON(serialize=false) //json不序列化
	public List getXkList() {
		return xkList;
	}
	public void setXkList(List xkList) {
		this.xkList = xkList;
	}
	@JSON(serialize=false) //json不序列化
	public String getYear() {
		return year;
	}
	@JSON(serialize=false) //json不序列化
	public IBasicInfoService getBasicInfoService() {
		return basicInfoService;
	}
	public void setBasicInfoService(IBasicInfoService basicInfoService) {
		this.basicInfoService = basicInfoService;
	}
	
	@JSON(serialize=false) //json不序列化
	public MailSettingForm getMsf() {
		return msf;
	}
	public void setMsf(MailSettingForm msf) {
		this.msf = msf;
	}
	@JSON(serialize=false) //json不序列化
	public String[] getEmails() {
		return emails;
	}
	public void setEmails(String[] emails) {
		this.emails = emails;
	}
	public String getEmailsString() {
		return emailsString;
	}
	public void setEmailsString(String emailsString) {
		this.emailsString = emailsString;
	}
	
	
  



}
